<?php

/**
 * Description of language
 *
 * @author libaoling
 * @time 2015-2-13
 */
class LanguageApp extends BackendApp
{

    private $_language_mod;

    function __construct()
    {
        $this->LanguageApp();
    }

    function LanguageApp()
    {
        parent::__construct();

        $this->_language_mod = & m('language');
    }
    
    public function index()
    {
        $languages = $this->_language_mod->get_list();
        
        $this->assign('languages', $languages);
        
        $this->import_resource(array('script' => 'inline_edit.js'));
        $this->display('language.index.html');
    }
    
    public function add()
    {
        if (!IS_POST)
        {
            $this->import_resource('jquery.plugins/jquery.validate.js');
            $this->display('language.form.html');
        }
        else
        {
            $data = array();
            $default_lang = $this->_language_mod->get_default_lang();
            $data['name'] = trim($_POST['name']);
            $data['lang'] = trim($_POST['lang']);
            $data['default_lang'] = $default_lang ? intval($_POST['default_lang']) : 1;
            $lang_id = $this->_language_mod->add($data);
            if (!$lang_id)
            {
                $this->show_warning($this->_language_mod->get_error());
                return;
            }
            
            $this->show_message('添加成功', 'back_list', 'index.php?app=language', '继续添加', 'index.php?app=language&amp;act=add');
        }
    }
    
    public function ajax_col()
    {
        $id = empty($_GET['id']) ? 0 : intval($_GET['id']);
        $column = empty($_GET['column']) ? '' : trim($_GET['column']);
        $value = isset($_GET['value']) ? trim($_GET['value']) : '';
        $data = array();

        if (in_array($column, array('name', 'lang')))
        {
            $data[$column] = $value;
            $this->_language_mod->edit($id, $data);
            if (!$this->_language_mod->has_error())
            {
                echo ecm_json_encode(true);
            }
        }
        else
        {
            return;
        }
        return;
    }
    
    public function drop()
    {
        $lang_ids = isset($_GET['id']) ? trim($_GET['id']) : 0;
        if (!$lang_ids)
        {
            $this->show_warning('请选择要删除的数据');

            return;
        }
        $lang_ids = explode(',', $lang_ids); //获取一个类似array(1, 2, 3)的数组
        if (!$this->_language_mod->drop($lang_ids))    //删除
        {
            $this->show_warning($this->_language_mod->get_error());

            return;
        }
        
        $this->show_message('删除成功');
    }
    
    public function set_default()
    {
        $id = empty($_GET['id']) ? 0 : intval($_GET['id']);
        
        if($this->_language_mod->edit($id, array('default_lang' => 1))){
            $this->_language_mod->edit("lang_id != '{$id}'", array('default_lang' => 0));
        }
        header("Location:index.php?app=language");
    }

}
